package com.yiban.demo.Global.Configuration;

import com.yiban.demo.LoginUser.Entity.LoginUserPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

@Configuration
@Slf4j
/**
 * 都需要登录
 */
public class WebsocketConfig extends ServerEndpointConfig.Configurator {
    //因为websocket借助http协议一部分握手，此时将session（http的session）中的身份信息绑定
    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        HttpSession session = (HttpSession) request.getHttpSession();
        if (sec != null) {
            //todo 注释掉是为了好测试，上线前要去掉
//            sec.getUserProperties().put("user",session.getAttribute("user"));
            sec.getUserProperties().put("user",new LoginUserPO(10,"sad","dsa","ads",true,"admin","1151"));
        }
       // super.modifyHandshake(sec, request, response);
    }
    @Bean
    public ServerEndpointExporter serverEndpointExporter(){
        return new ServerEndpointExporter();
    }

}
